package atguigu.shijianduanquchongbingleijia;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import lombok.Data;

@Data
public class TimeBucket {

    private Date start;
    private Date end;

    public TimeBucket(){};

    public TimeBucket(Date start, Date end) {
        if (start.after(end)) {
            throw new IllegalArgumentException("时间段无效(开始日期需要小于结束日期)");
        }
        this.start = start;
        this.end = end;
    }

    public TimeBucket(String start, String end) {
        SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            this.start = date.parse(start);
            this.end = date.parse(end);
        } catch (ParseException e) {
            e.printStackTrace();

            throw new IllegalArgumentException("时间段无效");
        }
    }

    public Date getStart() {
        return start;
    }

    public Date getEnd() {
        return end;
    }

    public long getStartTime() {
        return start.getTime();
    }

    public long getEndTime() {
        return end.getTime();
    }

    @Override
    public String toString() {
        return "TimeBucket{" +
                "start=" + start +
                ", end=" + end +
                '}';
    }
}
